﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using forever.Gobal;
using forever.ToolForFileXML;
using gencode.UI;

namespace gencode
{
    public partial class GenCode : Form
    {

        private ProgressBarControl progressBar;
        private DataTable dt;
        private int total;
        XtraTabControl xtraTabControl1;
        int index = -1;
        private string dataBase;
        private string user = "sa";
        private string passWord = "123";
        private string serverMame = "forever-pc";
        private string connectString = "server=forever-pc;uid=sa;pwd=123";
        public GenCode()
        {
            InitializeComponent();
            ConfigurationSettings.AppSettings.Set("connect", connectString);
        }

        private void GenCode_Load(object sender, EventArgs e)
        {
            init();
            progressBar = new ProgressBarControl();
            //ReflectorBar reflectorBar = new ReflectorBar(progressBar);

            // Add a progress bar to a form and show it.
            this.Controls.Add(progressBar);
            progressBar.Dock = DockStyle.Fill;
            xtraTabControl1 = new XtraTabControl();
            //
            ucGenerateByListSproc uc = new ucGenerateByListSproc("StudentHostel");
            uc.Text = "Generate by database - StudentHostel";
            addControlTabpage(uc);
        }
        void init()
        {
            trvDatabase.Nodes.Clear();
            TreeNode tree = new TreeNode(serverMame.ToUpper(), 24, 24);
            trvDatabase.Nodes.Add(tree);
            trvDatabase.SelectedImageIndex = -1;
            trvDatabase.ImageIndex = -1;
            //tabAction.TabPages.Clear();
        }
        internal void showControl(Control obj, Control panel)
        {
            panel.Controls.Clear();
            obj.Dock = DockStyle.Fill;
            panel.Controls.Add(obj);
        }
        public void addControlTabpage(Control obj)
        {
            index = -1;
            for (int i = 0; i < xtraTabControl1.TabPages.Count; i++)
            {
                if (obj.Text.Equals(xtraTabControl1.TabPages[i].Text))
                {
                    index = i;
                    break;
                }
            }
            if (index < 0)
            {
                XtraTabPage tabpage = new XtraTabPage();
                obj.Dock = DockStyle.Fill;
                tabpage.Text = obj.Text;
                tabpage.Controls.Add(obj);
                showStart(tabpage);
                xtraTabControl1.SelectedTabPageIndex = xtraTabControl1.TabPages.Count - 1;
            }
            else
            {
                showControl(xtraTabControl1, pnlControls);
                xtraTabControl1.SelectedTabPageIndex = index;
            }
        }
        void showStart(XtraTabPage tabpage1)
        {
            if (xtraTabControl1.TabPages.Count < 1)
            {
                xtraTabControl1 = new XtraTabControl();
            }
            tabpage1.ShowCloseButton = DevExpress.Utils.DefaultBoolean.True;
            xtraTabControl1.TabPages.Add(tabpage1);
            xtraTabControl1.ClosePageButtonShowMode = ClosePageButtonShowMode.InAllTabPageHeaders;
            xtraTabControl1.CloseButtonClick += new EventHandler(xtraTabControl1_CloseButtonClick);
            xtraTabControl1.LookAndFeel.SkinName = "Money Twins";
            xtraTabControl1.SelectedTabPage = tabpage1;
            pnlControls.Controls.Add(xtraTabControl1);
            showControl(xtraTabControl1, pnlControls);
        }
        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            XtraTabControl tabControl = sender as XtraTabControl;
            ClosePageButtonEventArgs arg = e as ClosePageButtonEventArgs;
            //(arg.Page as XtraTabPage).PageVisible = false;
            (arg.Page as XtraTabPage).Dispose();
            //tabControl.Dispose();
            if (tabControl.TabPages.Count < 1)
            {
                //home
            }
        }
        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            int level = tree.Level;
            if (level == 0)
            {
                tree.Nodes.Clear();
                dt = Gobal.SelectDatabaseNames();
                int total = dt.Rows.Count;
                for (int i = 0; i < total; i++)
                {
                    tree.Nodes.Add(dt.Rows[i]["NAME"].ToString(), dt.Rows[i]["NAME"].ToString(), 6, 6);
                }
            }
            else if (level == 1)
            {
                tree.Nodes.Clear();
                tree.Nodes.Add("Tables", "Tables", 11, 11);
                tree.Nodes.Add("Views", "Views", 11, 11);
                tree.Nodes.Add("Programmability", "Programmability", 11, 11);
            }
            else if (level == 2)
            {
                TreeNode chil;
                tree.Nodes.Clear();
                dataBase = tree.Parent.Name;
                switch (tree.Name)
                {
                    case "Tables":
                        dt = Gobal.SelectTableNames(dataBase);
                        int total = dt.Rows.Count;
                        for (int i = 0; i < total; i++)
                        {
                            chil = tree.Nodes.Add(dt.Rows[i]["TABLE_NAME"].ToString(), dt.Rows[i]["TABLE_NAME"].ToString(), 38, 38);
                            chil.Nodes.Add("Columns", "Columns", 11, 11);
                            chil.Nodes.Add("Keys", "Keys", 11, 11);
                            chil.Nodes.Add("Constraints", "Constraints", 11, 11);
                            chil.Nodes.Add("Triggers", "Triggers", 11, 11);
                            chil.Nodes.Add("Indexes", "Indexes", 11, 11);
                            chil.Nodes.Add("Statistics", "Statistics", 11, 11);
                        }
                        break;
                    case "Views":
                        dt = Gobal.SelectViewNames(dataBase);
                        total = dt.Rows.Count;
                        for (int i = 0; i < total; i++)
                        {
                            chil = tree.Nodes.Add(dt.Rows[i]["TABLE_NAME"].ToString(), dt.Rows[i]["TABLE_NAME"].ToString(), 38, 38);
                            chil.Nodes.Add("Columns", "Columns", 11, 11);
                            chil.Nodes.Add("Keys", "Keys", 11, 11);
                            chil.Nodes.Add("Triggers", "Triggers", 11, 11);
                            chil.Nodes.Add("Statistics", "Statistics", 11, 11);
                        }
                        break;
                    case "Programmability":
                        tree.Nodes.Add("Stored Procedures", "Stored Procedures", 11, 11);
                        tree.Nodes.Add("Functions", "Functions", 11, 11);
                        break;

                }
            }
            else if (level == 3)
            {
                tree.Nodes.Clear();
                dataBase = tree.Parent.Parent.Name;
                switch (tree.Parent.Name)
                {
                    #region tables
                    case "Tables":
                        switch (tree.Name)
                        {
                            case "Columns":
                                dt = Gobal.SelectColumnNames(dataBase, tree.Parent.Name);
                                total = dt.Rows.Count;
                                string ten = "";
                                for (int i = 0; i < total; i++)
                                {
                                    ten = dt.Rows[i]["COLUMN_NAME"] + " (" + dt.Rows[i]["DATA_TYPE"] + "";
                                    if (!dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"].ToString().Equals(""))
                                    {
                                        ten += "(" + dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"] + ")";
                                    } if (dt.Rows[i]["IS_NULLABLE"].ToString() == "YES")
                                    {
                                        ten += ", null)";
                                    }
                                    else
                                    {
                                        ten += ",not null)";
                                    }
                                    tree.Nodes.Add(ten, ten, 40, 40);
                                }
                                break;
                            case "Keys":
                                dt = Gobal.SelectColumnsByConstraintType(dataBase, tree.Parent.Name, "");
                                total = dt.Rows.Count;
                                for (int i = 0; i < total; i++)
                                {
                                    tree.Nodes.Add(dt.Rows[i]["COLUMN_NAME"].ToString(), dt.Rows[i]["COLUMN_NAME"].ToString(), 43, 43);
                                }
                                break;
                            case "Constraints":
                                dt = Gobal.SelectColumnsByConstraintCheck(dataBase, tree.Parent.Name);
                                total = dt.Rows.Count;
                                for (int i = 0; i < total; i++)
                                {
                                    tree.Nodes.Add(dt.Rows[i]["CONSTRAINT_NAME"].ToString(), dt.Rows[i]["CONSTRAINT_NAME"].ToString(), 45, 45);
                                }
                                dt = Gobal.SelectDefaultColumns(dataBase, tree.Parent.Name);
                                total = dt.Rows.Count;
                                for (int i = 0; i < total; i++)
                                {
                                    tree.Nodes.Add(dt.Rows[i]["conscolumn"].ToString() + " (" + dt.Rows[i]["columns"] + ")", dt.Rows[i]["conscolumn"].ToString() + " (" + dt.Rows[i]["columns"] + ")", 46, 46);
                                }
                                break;
                            case "Triggers":
                                break;
                            case "Indexes":
                                break;
                            case "Statistics":
                                break;
                        }

                        break;
                    #endregion
                    #region view
                    case "Views":
                        switch (tree.Name)
                        {
                            case "Columns":
                                dt = Gobal.SelectColumnNames(dataBase, tree.Parent.Name);
                                total = dt.Rows.Count;
                                string ten = "";
                                for (int i = 0; i < total; i++)
                                {
                                    ten = dt.Rows[i]["COLUMN_NAME"] + " (" + dt.Rows[i]["DATA_TYPE"] + "";
                                    if (!dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"].ToString().Equals(""))
                                    {
                                        ten += "(" + dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"] + ")";
                                    } if (dt.Rows[i]["IS_NULLABLE"].ToString() == "YES")
                                    {
                                        ten += ", null)";
                                    }
                                    else
                                    {
                                        ten += ",not null)";
                                    }
                                    tree.Nodes.Add(ten, ten, 40, 40);
                                }
                                break;
                            case "Triggers":
                                break;
                            case "Indexes":
                                break;
                            case "Statistics":
                                break;
                        }
                        break;
                    #endregion
                    #region Programmability
                    case "Programmability":
                        TreeNode chil;
                        switch (tree.Name)
                        {
                            case "Stored Procedures"://"Stored Procedures":
                                dt = Gobal.SelectSprocNames(dataBase);
                                int total = dt.Rows.Count;
                                for (int i = 0; i < total; i++)
                                {
                                    chil = tree.Nodes.Add(dt.Rows[i]["SPECIFIC_NAME"].ToString(), dt.Rows[i]["SPECIFIC_NAME"].ToString(), 40, 40);
                                }
                                break;
                            case "Functions"://"Functions":

                                break;
                        }
                        break;
                    #endregion
                }
            }
            if (tree.IsExpanded != false)
            {
                tree.Expand();
            }
            else
            {
                tree.Collapse();
            }
        }

        private void refeshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            tree.Nodes.Clear();
            dt = Gobal.SelectDatabaseNames();
            int total = dt.Rows.Count;
            for (int i = 0; i < total; i++)
            {
                tree.Nodes.Add(dt.Rows[i]["NAME"].ToString(), dt.Rows[i]["NAME"].ToString(), 6, 6);
            }
            tree.Expand();
        }

        private void trvDatabase_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            int level = tree.Level;
            switch (level)
            {
                case 0:
                    trvDatabase.ContextMenuStrip = mnuServer;
                    break;
                case 1:
                    trvDatabase.ContextMenuStrip = mnuDatabase;
                    break;
                case 2:
                    if (tree.Name.Equals("Tables"))
                    {
                        trvDatabase.ContextMenuStrip = mnuTables;
                    }
                    else if (tree.Name.Equals("Views"))
                    {
                        trvDatabase.ContextMenuStrip = mnuViews;
                    }
                    else
                    {

                    }
                    break;
                case 3:
                    if (tree.Parent.Name.Equals("Tables"))
                    {
                        tree.ContextMenuStrip = mnuTable;
                    }
                    else if (tree.Parent.Name.Equals("Views"))
                    {
                        tree.ContextMenuStrip = mnuView;
                    }
                    else if (tree.Name.Equals("Stored Procedures"))
                    {
                        tree.ContextMenuStrip = mnuSprocs;
                    }
                    else if (tree.Name.Equals("Functions"))
                    {
                    }
                    break;
                case 4:
                    if (tree.Parent.Parent.Name.Equals("Tables"))
                    {
                        switch (tree.Name)
                        {
                            case "Columns":
                                break;
                            case "Keys":
                                break;
                            case "Constraints":
                                break;
                            case "Triggers":
                                break;
                            case "Indexes":
                                break;
                            case "Statistics":
                                break;
                        }
                    }
                    else if (tree.Parent.Parent.Name.Equals("Views"))
                    {
                        switch (tree.Name)
                        {
                            case "Columns":
                                break;
                            case "Triggers":
                                break;
                            case "Indexes":
                                break;
                            case "Statistics":
                                break;
                        }
                    }
                    else if (tree.Parent.Name.Equals("Stored Procedures"))
                    {
                        tree.ContextMenuStrip = mnuSproc;
                    }
                    else if (tree.Parent.Name.Equals("Functions"))
                    {
                    }
                    break;
            }
        }
        private void mnuDatabase_Opening(object sender, CancelEventArgs e)
        {

        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void generateThreeLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            string ten = tree.Name;
            ucGenerateByTable uc = new ucGenerateByTable(tree.Parent.Parent.Name, tree.Name);
            uc.Text = "Generate by table " + ten;
            addControlTabpage(uc);
        }

        private void refeshToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            string ten = tree.Name;
            ucGenerateProject uc = new ucGenerateProject(tree.Name);
            uc.Text = "Generate by project - " + ten;
            addControlTabpage(uc);
        }
        private void generateCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            string ten = tree.Name;
            ucGenerateBySproc uc = new ucGenerateBySproc(tree.Parent.Parent.Parent.Name, tree.Name);
            uc.Text = "Generate by sproc " + ten;
            addControlTabpage(uc);
        }

        private void generateListStoredProceduresToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tree = trvDatabase.SelectedNode;
            string ten = tree.Name;
            ucGenerateByListSproc uc = new ucGenerateByListSproc(tree.Parent.Parent.Parent.Name);
            uc.Text = "Generate by list sproc of Database " + ten;
            addControlTabpage(uc);
        }
    }
}
